# -*- coding: utf-8 -*-
"""
Created on Thu Oct 14 14:48:51 2021

@author: lenovo
"""
from skimage import io,filters
from matplotlib import pyplot as plt
import numpy as np
#图像空间滤波函数
def corre12d(img,window):
    m=window.shape[0]
    n=window.shape[1]
    #边界通过0灰度值填充扩展
    img1=np.zeros((img.shape[0]+m-1,img.shape[1]+n-1))
    img1[(m-1)//2:(img.shape[0]+(m-1)//2),(n-1)//2:(img.shape[1]+(n-1)//2)]=img
    img2=np.zeros(img.shape)
    for i in range(img2.shape[0]):
        for j in range(img2.shape[1]):
            temp=img1[i:i+m,j:j+n]
            img2[i,j]=np.sum(np.multiply(temp,window))
            return img2
        #img为原始图像
        img=io.imread('boneScan.tif',as_gray=True)
        #中心系数为-2
        window=np.array([[0,1,0],[0,-2,0],[0,1,0]])
        img_laplace=corre12d(img,window)
        img_laplace=255*(img_laplace-img_laplace.min())/(img_laplace.max()-img_laplace.min())
       # img_laplace_enhance=img+img_laplace
        img_sobel=filters.sobel(img)
        imgList=[img, img_laplace, img_sobel]
        for grayImg in imgList:
            plt.figure()
            plt.axis('off')
            plt.imshow(grayImg,cmap='gray')
        
        
        
        
#中心系数为-4        
        window1=np.array([[0,1,0],[1,-4,1],[0,1,0]])
        img_laplace1=corre12d(img,window1)
        img_laplace1=255*(img_laplace1-img_laplace1.min())/(img_laplace1.max()-img_laplace1.min())
       # img_laplace_enhance=img+img_laplace
        img_sobel1=filters.sobel(img)
        imgList1=[img, img_laplace1, img_sobel1]
        for grayImg1 in imgList1:
            plt.figure()
            plt.axis('off')
            plt.imshow(grayImg1,cmap='gray')
            
            
            
#中心系数为-8        
        window2=np.array([[-1,-1,-1],[-1,8,-1],[-1,-1,-1]])
        img_laplace12=corre12d(img,window2)
        img_laplace12=255*(img_laplace12-img_laplace12.min())/(img_laplace12.max()-img_laplace12.min())
       # img_laplace_enhance=img+img_laplace
        img_sobel2=filters.sobel(img)
        imgList2=[img, img_laplace12, img_sobel2]        
        for grayImg2 in imgList2:
            plt.figure()
            plt.axis('off')
            plt.imshow(grayImg2,cmap='gray')            
